﻿using System;
using System.Linq;
using Wayloop.Blog.Core.Data;
using Wayloop.Blog.Core.Data.Entities;


namespace Wayloop.Blog.DataAccess.Repositories
{
    public class PostRepository : AbstractRepository<Post>, IPostRepository
    {
        public override IQueryable<Post> FindAll()
        {
            return from post in base.FindAll()
                   orderby post.CreatedOn descending
                   select post;
        }


        public Post Find(string slug)
        {
            var posts = from post in FindAll()
                        where post.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase)
                        select post;

            return posts.SingleOrDefault();
        }


        public IQueryable<Post> FindAllPublished()
        {
            return from post in FindAll()
                   where post.PublishedOn != null
                   select post;
        }


        public IQueryable<Post> FindAllPublished(int pageIndex, int pageSize)
        {
            return FindAllPublished()
                .Skip(pageIndex * pageSize)
                .Take(pageSize);
        }
    }
}